﻿using APIShoppingSystem.GetSession;
using APIShoppingSystem.Model;
using IShoppingBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RepositoryCommon;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace APIShoppingSystem.Controllers
{
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class LoginController : ControllerBase
	{
		IUserInfoBll _userInfoBll;

		public LoginController(IUserInfoBll userInfoBll)
		{
			_userInfoBll = userInfoBll;
		}

        /// <summary>
        /// 登录验证
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Login([FromForm]string account, [FromForm]string password)
        {
            CustomActionResult res = new CustomActionResult();
            //参数认证
            if (string.IsNullOrEmpty(account))
            {
                res.Msg = "账号不能为空";
                return new JsonResult(res);
            }
            else if (string.IsNullOrEmpty(password))
            {
                res.Msg = "密码不能为空";
                return new JsonResult(res);
            }

            //使用MD5加密
            string pwdMD5 = MD5Helper.MD5Encrypt64(password);

            //判断账号密码是否正确
            UserInfo userInfo = _userInfoBll.GetUserInfoByLogin(account, pwdMD5);
            if (userInfo == null)
            {
                res.Msg = "账号或密码错误";
                return new JsonResult(res);
            }
            else
            {
                res.IsSuccess = true;
                res.Status = 1;
                res.Msg = "登录成功";
                res.Datas = new
                {
                    UserName = userInfo.UserName
                };
                //序列化用户对象
                string jsonUserInfo = JsonConvert.SerializeObject(userInfo);

                //保持用户信息到session中
                HttpContext.Session.SetString("UserInfo", jsonUserInfo);

                return new JsonResult(res);
            }
        }



        /// <summary>
        /// 判断当前登录用户是否是管理员
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult IsAdmin()
        {
            CustomActionResult res = new CustomActionResult();

            UserInfo userInfo = this.GetSessionUserInfo();

            if (userInfo != null)
            {
                if (userInfo.IsAdmin)
                {
                    res.IsSuccess = true;
                    res.Status = 1;
                    res.Msg = "是管理员";
                }
            }

            return new JsonResult(res);
        }


        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Logout()
        {
            CustomActionResult res = new CustomActionResult();
            //删除当前登录用户的session
            HttpContext.Session.Remove("UserInfo");
            res.IsSuccess = true;
            res.Msg = "成功";
            res.Status = 1;
            return new JsonResult(res);
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="oldPassword"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult EditPassword([FromForm] string oldPassword, [FromForm] string newPassword)
        {
            CustomActionResult res = new CustomActionResult();

            if (string.IsNullOrEmpty(oldPassword) || string.IsNullOrEmpty(newPassword))
            {
                res.Msg = "请输入新、旧密码";
                return new JsonResult(res);
            }
            UserInfo userInfo = this.GetSessionUserInfo();
            if (userInfo == null)
            {
                res.Msg = "未获取到当前的用户信息";
                return new JsonResult(res);
            }
            //使用md5加密
            string oldpwdMd5 = MD5Helper.MD5Encrypt64(oldPassword);
            string newpwdMd5 = MD5Helper.MD5Encrypt64(newPassword);

            string msg = "";
            bool isSuccess = _userInfoBll.EditPassword(oldpwdMd5, newpwdMd5, userInfo, out msg);
            if (isSuccess)
            {
                res.Status = 1;
                res.IsSuccess = true;
                //删除当前用户的session
                HttpContext.Session.Remove("UserInfo");
            }
            res.Msg = msg;
            return new JsonResult(res);
        }
    }
}
